/*
** Copyright (c) 2008 Mobile GCalendar
**
** Permission is hereby granted, free of charge, to any person
** obtaining a copy of this software and associated documentation
** files (the "Software"), to deal in the Software without
** restriction, including without limitation the rights to use,
** copy, modify, merge, publish, distribute, sublicense, and/or sell
** copies of the Software, and to permit persons to whom the
** Software is furnished to do so, subject to the following
** conditions:
**
** The above copyright notice and this permission notice shall be
** included in all copies or substantial portions of the Software.
**
** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
** OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
** NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
** HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
** WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
** FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
** OTHER DEALINGS IN THE SOFTWARE.
*/

package au.edu.mq.comp.itec800.mgc.lib;

import java.util.List;

import au.edu.mq.comp.itec800.mgc.lib.calendar.Colors;
import au.edu.mq.comp.itec800.mgc.util.GDataUtils;
import au.edu.mq.comp.itec800.mgc.util.StringUtils;

import com.google.gdata.data.BaseEntry;
import com.google.gdata.data.calendar.CalendarEntry;
import com.google.gdata.data.calendar.HiddenProperty;
import com.google.gdata.data.extensions.Where;

/**
 * A extension class for the GData API's CalendarEntry.
 *
 * This class provides a few more simplistic editing features for the
 * CalendarEntry and makes it easier to use by external software.
 *
 *
 * This classes calls converting and wrapping methods from the
 * mgc.util.GDataUtils class as well.
 *
 *
 * @see CalendarEntry
 * @see GDataUtils
 *
 * @author Laurent Malvert [laurent.malvert@students.mq.edu.au]
 */
public class GCalendar extends CalendarEntry
{
  public GCalendar()
  {
    super();
  }

  public GCalendar(final BaseEntry<CalendarEntry> entry)
  {
    super(entry);
  }

  public GCalendar(final CalendarEntry entry)
  {
    this((BaseEntry<CalendarEntry>) entry);
  }

  public void         updateName(final String name)
  {
    if (name != null)
    {
      setTitle(GDataUtils.toPlainTextConstruct(name));
    }
  }

  public void         updateDescription(final String description)
  {
    if (description != null)
    {
      setSummary(GDataUtils.toPlainTextConstruct(description));
    }
  }

  public void         updateColor(final Colors color)
  {
    if (color != null)
    {
      setColor(GDataUtils.toColorProperty(color.getRGB()));
    }
  }

  public void         updateColor(final String color)
  {
    if (color != null)
    {
      final String  cleanColor  = StringUtils.clean(color);

      if (cleanColor.startsWith("#"))
      {
        // we explicitly convert back from RGB to RGB to ensure the
        // user-supplied color is part of the supported palette.
        setColor(GDataUtils.toColorProperty(Colors.fromRGB(cleanColor).getRGB()));
      }
      else
      {
        setColor(GDataUtils.toColorProperty(Colors.fromId(cleanColor).getRGB()));
      }
    }
  }

  public void         updateTimeZone(final String timezone)
  {
    if (timezone != null)
    {
      setTimeZone(GDataUtils.toTimeZoneProperty(timezone));
    }
  }

  public void         updateVisibility(final String visibility)
  {
    if (visibility != null)
    {
      setHidden(GDataUtils.toHiddenProperty(visibility));
    }
  }

  public void         updateLocation(final String location)
  {
    if (location != null)
    {
      getLocations().clear();
      addLocation(new Where("", "", location));
    }
  }

  public String       getShortId()
  {
    return (GDataUtils.getSafeId(this));
  }

  public boolean      isSameAs(final GCalendar cal2)
  {
    return ((cal2 != null) ? getShortId().equals(cal2) : false);
  }

  public boolean      isHidden()
  {
    return (getHidden().equals(HiddenProperty.TRUE));
  }

  public boolean      isOwn()
  {
    return (getId().indexOf("/own") != -1);
  }

  public String       getLocation()
  {
    final List<Where>                       locations = getLocations();
    String                                  location  = "";

    if (locations.size() != 0)
    {
      location = StringUtils.sanitize(locations.get(0).getValueString());
    }
    return (location);
  }

  public String       getDescription()
  {
    return (GDataUtils.fromTextConstruct(getSummary()));
  }

  public String       getTimezone()
  {
    return (GDataUtils.fromTimeZoneProperty(getTimeZone()));
  }

  public String       getName()
  {
    return (GDataUtils.fromTextConstruct(getTitle()));
  }

  public String       getColorCode()
  {
    return (GDataUtils.fromColorProperty(getColor()));
  }

  public String       getColorName()
  {
    return (Colors.fromRGB(GDataUtils.fromColorProperty(getColor())).getName());
  }

}
